package jointpart.assignment4;

import static org.junit.Assert.*;

import java.io.IOException;

import shared.*;

import org.junit.Test;

/**
 * This class provides test for following methods form Searcher:
 * 	hashMapFile
 * 
 * 
 */
public class Searcher4Test {
	
	// Test dataset for test 4A
	String dataSet4A = "data/test/dataSetB.txt"; // Contains only "*PAGE:1"
	WordList[] dataSet4AList = new WordList[4];
	
	// Test dataset for test 4B
	String dataSet4B = "data/test/dataSet4B.txt"; // Empty file

	// Test dataset for test 4C
	String dataSet4C = "data/test/dataSet4C.txt"; // One url, two words
	
	URLList page1 = new URLList("1", null);
	URLList page2 = new URLList("2", null);
	
	WordList your = new WordList("your", page1 , null);
	WordList bases = new WordList("bases", page1 , null);
	WordList are = new WordList("are", page1 , null);
	WordList belong = new WordList("belong", page1 , null);
	WordList to = new WordList("to", page1 , null);
	WordList us = new WordList("us", page1 , null);
	
	WordList[] dataSet4CList = new WordList[] {null, null, bases, your};
	
	// Test dataset for test 4D
	String dataSet4D = "data/test/dataSetF.txt";
	URLList page3 = new URLList("2", page1);
	WordList your4D = new WordList("your", page3 , to);	
	WordList bases4D = new WordList("bases", page3 , null);
	
	WordList[] dataSet4DList = new WordList[] {null, belong, bases4D, your4D, are, null, us, null};
	
	// Test dataset for 4E
	// One url and words with same hashKey
	String dataSet4E = "data/test/dataSet4E.txt"; 
	WordList i = new WordList("i", page1 , null);	
	WordList e = new WordList("e", page1 , i);
	WordList a = new WordList("a", page1 , e);
	
	WordList[] dataSet4EList = new WordList[] {null, a, null, null};
	
	// Test dataset for 4F
	String dataSet4F = "data/test/dataSetC.txt";
	WordList[] dataSet4FList = new WordList[] {null, null, null, your};
	
	// Test dataset for testResizeMapA - Array[2]
	WordList b = new WordList("b", page1 , null);
	WordList c = new WordList("c", page1 , null);
	
	WordList[] dataSetResizeA = new WordList[] {b, c};
	WordList[] expectedResizeA = new WordList[] {null, null, b, c };
	
	// Test dataset for testResizeMapB - Empty Array[4];
	WordList[] dataSetResizeB = new WordList[] {null, null, null, null};
	WordList[] expectedResizeB = new WordList[] {null,null, null, null, null, null, null, null};
	
	// Test dataset for testResizeMapB - Array[2] where new hashkey of two objects is the same;
	
	WordList f = new WordList("f", page1 , null);
	WordList fb = new WordList("f", page1, b);
	
	WordList[] dataSetResizeC = new WordList[] {b, f ,null, null};
	WordList[] expectedResizeC = new WordList[] { null, null, fb, null};
	

	@Test
	public void testHashMapFileA() throws IOException {
		assertArrayEquals(dataSet4AList, Searcher4.hashMapFile(dataSet4A, 4));
	}
	
	@Test
	public void testHashMapFileB() throws IOException {
		assertArrayEquals(dataSet4AList , Searcher4.hashMapFile(dataSet4B, 4));
	}
	
	@Test
	public void testHashMapFileC() throws IOException {
		assertArrayEquals(dataSet4CList, Searcher4.hashMapFile(dataSet4C, 4));
	}
	
	@Test
	public void testHashMapFileD() throws IOException {
		assertArrayEquals(dataSet4DList, Searcher4.hashMapFile(dataSet4D, 4));
	}
	
	@Test
	public void testHashMapFileE() throws IOException {
		assertArrayEquals(dataSet4EList, Searcher4.hashMapFile(dataSet4E, 4));
	}
	
	@Test
	public void testHashMapFileF() throws IOException {
		assertArrayEquals(dataSet4FList, Searcher4.hashMapFile(dataSet4F, 4));
	}

	
	// Test for resizeMap
	
	@Test
	public void testResizeMapA() throws IOException {
		assertArrayEquals(expectedResizeA, Searcher4.resizeMap(dataSetResizeA, 2));
	}
	
	@Test
	public void testResizeMapB() throws IOException {
		assertArrayEquals(expectedResizeB, Searcher4.resizeMap(dataSetResizeB, 4));
	}
	
	@Test
	public void testResizeMapC() throws IOException {
		assertArrayEquals(expectedResizeC, Searcher4.resizeMap(dataSetResizeC, 2));
	}
	
}
